package testes.vSys.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import testes.vSys.bean.Brand;
import testes.vSys.bean.CarType;
import testes.vSys.bean.Model;

public class _stub_ModelDao implements IModelDao{

	private HashMap<Integer, Model> models;
	private HashMap<Integer, Brand> brands;
	private HashMap<Integer, Collection<Model>> models_x_brand;
	
	private int currentBrandId;
	private int currentModelId;
	
	public Brand createBrand(Brand brand) {
		// TODO Auto-generated method stub
		Brand retBrand = new Brand(++currentBrandId,brand.getBrand());
		brands.put(retBrand.getId(),retBrand);
		return retBrand;
	}

	public Model createModel(Model model) {
		// TODO Auto-generated method stub
		currentModelId++;
		Collection<Model> m = models_x_brand.get(model.getBrand().getId());
		if(m==null)
			return null;
		Model tmp = models.put(currentModelId,new Model(currentModelId,model.getBrand(),model.getCarType(),model.getModel()));
		m.add(tmp);
		return tmp;
	}

	public boolean removeBrand(int brandId) {
		// TODO Auto-generated method stub
		models_x_brand.remove(brandId);
		return (brands.remove(brandId)!=null);
	}

	public boolean removeModel(int modelId) {
		// TODO Auto-generated method stub
		Model model = models.get(modelId);
		int brandId = model.getBrand().getId();
		if (models.remove(modelId)!=null) {
			// Atulizar a coleçao dos modelos
			Collection<Model> m = models_x_brand.remove(brandId);
			if (m == null)
				return false;
			
			Iterator<Model> i = m.iterator();
			Model m2 = null;
			while(i.hasNext()) {
				m2 = i.next();
				if(m2.getId()==modelId) {
					i.remove();
					break;
				}
			}
			
			models_x_brand.put(brandId,m);
			return true;
		}
		return false;
	}

	
	public Collection<Brand> getAvaiableBrands() {
		Collection<Brand> tmp = brands.values();
		if (tmp.isEmpty())
			return null;
		return tmp;
	}

	public Collection<Model> getAvaiableModels() {
		Collection<Model> tmp = models.values();
		if (tmp.isEmpty())
			return null;
		return tmp;	
	}

	public Brand getBrandById(int brandId) {
		return brands.get(brandId);
	}

	public Collection<Model> getModelsByBrand(Brand brand) {
		return models_x_brand.get(brand.getId());
	}

	public Model getModelById(int modelId) {
		return models.get(modelId);
	}

	
	public _stub_ModelDao() {
		this.currentBrandId = 0;
		this.currentModelId = 0;
		
		models = new HashMap<Integer, Model>(); 
		brands = new HashMap<Integer, Brand>();
		models_x_brand = new HashMap<Integer, Collection<Model>>();
		Collection<Model> m1 = new ArrayList<Model>();

		models.put(1, new Model(1,new Brand(1,"Ford"),null,"Fiesta"));
		m1.add(new Model(1,new Brand(1,"Ford"),null,"Fiesta"));

		models.put(2, new Model(2,new Brand(1,"Ford"),null,"Courier"));
		m1.add(new Model(2,new Brand(1,"Ford"),null,"Courier"));

		models.put(3, new Model(3,new Brand(1,"Ford"),null,"Focus"));
		m1.add(new Model(3,new Brand(1,"Ford"),null,"Focus"));

		models.put(4, new Model(4,new Brand(1,"Ford"),null,"Ka"));
		m1.add(new Model(4,new Brand(1,"Ford"),null,"Ka"));

		models_x_brand.put(1, m1);
		
		
		Collection<Model> m2 = new ArrayList<Model>();

		models.put(5, new Model(5,new Brand(2,"Fiat"),null,"Palio"));
		m2.add(new Model(5,new Brand(2,"Fiat"),null,"Palio"));

		models.put(6, new Model(6,new Brand(2,"Fiat"),null,"Strada"));
		m2.add(new Model(6,new Brand(2,"Fiat"),null,"Strada"));
		
		models.put(7, new Model(7,new Brand(2,"Fiat"),null,"Siena"));
		m2.add(new Model(7,new Brand(2,"Fiat"),null,"Siena"));
		
		models.put(8, new Model(8,new Brand(2,"Fiat"),null,"Uno"));
		m2.add(new Model(8,new Brand(2,"Fiat"),null,"Uno"));
		
		models.put(9, new Model(9,new Brand(2,"Fiat"),null,"Stilo"));
		m2.add(new Model(9,new Brand(2,"Fiat"),null,"Stilo"));
		
		models_x_brand.put(2, m2);
		
		
		Collection<Model> m3 = new ArrayList<Model>();
		
		models.put(10, new Model(10,new Brand(3,"Peugeot"),null,"206"));
		m3.add(new Model(10,new Brand(3,"Peugeot"),null,"206"));
		
		models.put(11, new Model(11,new Brand(3,"Peugeot"),null,"307"));
		m3.add(new Model(11,new Brand(3,"Peugeot"),null,"307"));
		
		currentModelId = 11;
		
		models_x_brand.put(3, m3);
		
		brands.put(1, new Brand(1,"Ford"));
		brands.put(2, new Brand(2,"Fiat"));
		brands.put(3, new Brand(3,"Peugeot"));
		
		currentBrandId = 3;
	
	}

	public Collection<CarType> getAvailableCarTypes() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
